package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: gmall-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-02 10:14
 **/
@Api(tags = "商品基础属性接口")
@RestController
@RequestMapping("/admin/product")
@RefreshScope
public class BaseManageController {

    @Autowired
    private ManageService manageService;

    @ApiOperation("根据平台属性Id 获取到平台属性值集合")
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId) {
        // 我们要完成业务的完整性
        //List<BaseAttrValue> baseAttrValues = manageService.getAttrValueList(attrId);

        BaseAttrInfo baseAttrInfo = manageService.getBaseAttrInfo(attrId);

        return Result.ok(baseAttrInfo.getAttrValueList());
    }

    @ApiOperation("保存-修改平台属性")
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        if (StringUtils.isEmpty(baseAttrInfo)) {
            return Result.fail();
        } else {
            manageService.saveAttrInfo(baseAttrInfo);
            return Result.ok();
        }
    }

    @ApiOperation("获取分类的一级")
    @GetMapping("getCategory1")
    public Result getCategory1() {

        //获取分类的一级
        List<BaseCategory1> baseCategory1List = manageService.findAll();

        //返回数据
        return Result.ok(baseCategory1List);
    }

    /**
     * 根据一级分类Id 查询二级分类数据
     *
     * @param category1Id
     * @return
     */
    @ApiOperation("根据一级分类Id 查询二级分类数据")
    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable("category1Id") Long category1Id) {

        List<BaseCategory2> baseCategory2List = manageService.getCategory2(category1Id);

        return Result.ok(baseCategory2List);
    }

    /**
     * 根据二级分类Id 查询三级分类数据
     *
     * @param category2Id
     * @return
     */
    @ApiOperation("根据二级分类Id 查询三级分类数据")
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable("category2Id") Long category2Id) {
        List<BaseCategory3> baseCategory3List = manageService.getCategory3(category2Id);

        return Result.ok(baseCategory3List);
    }

    /**
     * 根据分类Id 获取平台属性数据
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @ApiOperation("根据分类Id 获取平台属性数据")
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>> attrInfoList(@PathVariable("category1Id") Long category1Id,
                                                   @PathVariable("category2Id") Long category2Id,
                                                   @PathVariable("category3Id") Long category3Id) {

        List<BaseAttrInfo> baseAttrInfoList = manageService.attrInfoList(category1Id, category2Id, category3Id);

        return Result.ok(baseAttrInfoList);
    }

}
